package com.itheima.service.serviceImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.CheckGroupDao;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    CheckGroupDao checkGroupDao;

    @Override
    public void addCheckGroup(CheckGroup checkGroup) {
        checkGroupDao.addCheckGroup(checkGroup);
    }

    @Override
    public void deleteByIdCheckGroup(String id) {
        checkGroupDao.deleteCheckGroupAndSetMeal(id);

        checkGroupDao.deleteCheckGroupAndCheckItem(id);

        checkGroupDao.deleteByIdCheckGroup(id);
    }

    @Override
    public void editCheckGroup(CheckGroup checkGroup) {
        checkGroupDao.editCheckGroup(checkGroup);
    }

    @Override
    public CheckGroup findByIdCheckGroup(String id) {
        CheckGroup checkGroup = checkGroupDao.findByIdCheckGroup(id);
        return checkGroup;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());

        List<CheckGroup> list = checkGroupDao.findPageCheckGroup(queryPageBean.getQueryString());
        Page<CheckGroup> p = (Page<CheckGroup>) list;

        PageResult pageResult = new PageResult(p.getTotal(), p.getResult());
        return pageResult;
    }
}
